package helpers.pops
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.IList;
	import mx.controls.Button;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.FlexMouseEvent;
	import mx.events.ListEvent;
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;

	[Event(type="mx.events.ListEvent",name="change")]
	/**
	 * 四方向弹出的按钮列表 
	 * @author chzcb
	 * @email chzcb2008@gmail.com
	 * 
	 */
	public class PopUpBtnList extends Button
	{
		private var _datalist:IList;
		
		private var _labelField:String;
		
		private var _labelFunction:Function;
		
		/**
		 * up down left right
		 */
		private var _direction:String = "up";
		
		private var _selectIndex:int = -1;
		
		private var _selectItem:Object;
		
		private var _poplist:UIComponent;
		
		public function PopUpBtnList()
		{
			super();
			_poplist = new UIComponent();
			_poplist.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE,mousedownoutsideHandler);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,createdHandler);
		}
		
		private function mousedownoutsideHandler(event:FlexMouseEvent):void
		{
			this._poplist.visible = false;
		}
		
		override protected function clickHandler(event:MouseEvent):void
		{
			super.clickHandler(event);
			this._poplist.visible = true;
			this.changePopPostion();
		}
		
		private function resizeHandler(event:Event):void
		{
			this.changePopPostion();
		}
		
		private function createdHandler(event:FlexEvent):void
		{
			this.systemManager.addEventListener(Event.RESIZE,resizeHandler);
			drawList();
		}
		
		protected function drawList():void
		{
			if(_datalist&&_datalist.length>0){
				var i:int = 0;
				for(;i<_datalist.length;i++){
					var btn:Button = new Button();
					btn.styleName = this.styleName;
					btn.width = this.width;
					btn.height = this.height;
					if(Boolean(_labelFunction)){
						btn.label = _labelFunction(_datalist.getItemAt(i));
					}
					else if(_labelField)
						btn.label = _datalist.getItemAt(i)[this._labelField];
					else
						btn.label = _datalist.getItemAt(i) as String;
					switch(this._direction)
					{
						case "up":
							btn.x = 0;
							btn.y = -(i+1)*btn.height;
							break;
						case "down":
							btn.x = 0;
							btn.y = (i+1)*btn.height;
							break;
						case "left":
							btn.x = -(i+1)*btn.width;
							btn.y = 0;
							break;
						case "right":
							btn.x = (i+1)*btn.width;
							btn.y = 0;
							break;
					}
					btn.data = _datalist.getItemAt(i);
					btn.addEventListener(MouseEvent.CLICK,clickItemHandler);
					this._poplist.addChild(btn);
					this._poplist.visible = false;
					PopUpManager.addPopUp(this._poplist,this);
					changePopPostion();
				}
				if(_selectIndex>-1){
					selectIndex = _selectIndex;
				}
			}
		}
		
		private function clickItemHandler(event:MouseEvent):void
		{
			var index:int = this._datalist.getItemIndex(event.target.data);
			this.selectIndex = index;
			this._poplist.visible = false;
			this.dispatchEvent(new ListEvent(ListEvent.CHANGE,false,false,-1,index));
		}
		
		private function changePopPostion():void
		{
			var curp:Point = new Point(this.x,this.y);
			var globalp:Point = this.parent.localToGlobal(curp);
			this._poplist.x = globalp.x;
			this._poplist.y = globalp.y;
		}
		
		
		public function set dataProvider(value:IList):void
		{
			if(value){
				this._datalist = value;
			}
		}
		
		public function get dataProvider():IList
		{
			return this._datalist;
		}
		
		public function set direction(value:String):void
		{
			_direction = value;
		}
		
		public function get direction():String
		{
			return _direction;
		}

		public function get selectIndex():int
		{
			return _selectIndex;
		}

		public function set selectIndex(value:int):void
		{
			_selectIndex = value;
			if(this._datalist){
				if(value>-1 && value<this._datalist.length)
				{
					_selectItem = this._datalist.getItemAt(value);
				}
				if(Boolean(_labelFunction!=null)){
					this.label = _labelFunction(_selectItem);
				}
				else if(_labelField)
					this.label = _selectItem[this._labelField];
				else
					this.label = _selectItem as String;
			}
		}

		public function get selectItem():Object
		{
			return _selectItem;
		}

		public function set selectItem(value:Object):void
		{
			var index:int = this._datalist.getItemIndex(value);
			if(index == -1){
				return;
			}
			_selectItem = value;
			_selectIndex = index;
			if(_labelField)
				this.label = _selectItem[this._labelField];
			else
				this.label = _selectItem as String;
		}

		public function get labelField():String
		{
			return _labelField;
		}

		public function set labelField(value:String):void
		{
			_labelField = value;
		}

		public function get labelFunction():Function
		{
			return _labelFunction;
		}

		public function set labelFunction(value:Function):void
		{
			_labelFunction = value;
		}

		
	}
}